package linuxbox.bbs.taglib;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

import linuxbox.BoxStatic;
import linuxbox.bbs.logic.*;

/**
 * This class displays a bbs catalog menu list.
 */
public class BbsCatList extends TagSupport {
  protected String listType = null;
  protected int selected = -1;

  public int doStartTag() throws JspException {
    try {
      BbsManager bm = BbsManager.getInstance();
      BbsCat[] cats = bm.getAllCatalog();

      JspWriter out = pageContext.getOut();

      if ("table".equals(listType)) {
        for (int i = 0; i < cats.length; i++) {
          int id = cats[i].getID();
          //int pageNo = bm.getPageCount(BbsManager.PAGE_SIZE, id);
          out.println("<tr><td>");
          out.print("<a href=\"");
          out.print(BoxStatic.servletDir);
          out.print("BbsCatEditAction?CAT_ID=" + id + "\">"
                    + cats[i].getCatName() + "</a></td>");
          out.println("<td><a href=\"");
          out.print(BoxStatic.servletDir);
          out.print("BbsCatDelAction?CAT_ID=" + id + "\">Delete</a>");
          out.println("</td></tr>");
        }
      } else if ("list".equals(listType)) {
        StringBuffer retVal = new StringBuffer();

        for (int i = 0; i < cats.length; i++) {
          retVal.append("<OPTION value=" + cats[i].getID());

          if (cats[i].getID() == selected) {
            retVal.append(" SELECTED");
          }

          retVal.append(">" + cats[i].getCatName() + "</OPTION>");
        }
        out.println(retVal.toString());
      }
      out.flush();
    } catch(Exception e) {
      BoxStatic.logger.debug("Error : " + e.getMessage());
      e.printStackTrace();
      throw new JspException(e.getMessage());
    }

    return SKIP_BODY;
  }

  public void setListType(String aValue) {
    listType = aValue;
  }

  public void setSelected(String aValue) {
    selected = Integer.parseInt(aValue);
  }
}